<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>userInfo</title>
  <link rel="stylesheet" type="text/css" href="https://www.layuicdn.com/layui-v2.6.2/css/layui.css"/>

</head>
<body>
<form class="layui-form" lay-filter="username">
  <div class="layui-form-item">
    <label class="layui-form-label">用户名</label>
    <div class="layui-input-inline">
      <input id="username" type="text" name="username" readonly required lay-verify="required" autocomplete="off"
             class="layui-input">
    </div>
    <div class="layui-form-mid layui-word-aux">用户名不能更改</div>
  </div>
</form>
<form class="layui-form" lay-filter="info">
  <!-- id是隐藏的，但是提交时会一起提交给后台-->
  <input name="id" hidden>
  <div class="layui-form-item">
    <label class="layui-form-label">手机号</label>
    <div class="layui-input-inline">
      <input id="phone" type="tel" name="phone" required lay-verify="required|phone" placeholder="请输入手机号"
             autocomplete="off" class="layui-input">
    </div>
    <div class="layui-form-mid layui-word-aux">请输入手机号</div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">邮箱</label>
    <div class="layui-input-inline">
      <input type="text" name="email" required lay-verify="required|email" placeholder="请输入邮箱" autocomplete="off"
             class="layui-input">
    </div>
    <div class="layui-form-mid layui-word-aux">邮箱</div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">性别</label>
    <div class="layui-input-inline">
      <input type="radio" name="gender" value="男" title="男" checked>
      <input type="radio" name="gender" value="女" title="女">
    </div>

  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button type="submit" lay-filter="info-submit" class="layui-btn" lay-submit>立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>
<form class="layui-form" lay-filter="password">
  <input name="id" hidden>
  <div class="layui-form-item">
    <label class="layui-form-label">旧密码</label>
    <div class="layui-input-inline">
      <input id="oldPassword" type="password" name="oldPassword" required lay-verify="required|pass" placeholder="请输入密码"
             autocomplete="off" class="layui-input">
    </div>
    <div class="layui-form-mid layui-word-aux">请输入6-12密码</div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">新密码</label>
    <div class="layui-input-inline">
      <input id="newPassword" type="password" name="newPassword" required lay-verify="required|pass"
             placeholder="请输新入密码"
             autocomplete="off" class="layui-input">
    </div>
    <div class="layui-form-mid layui-word-aux">请输入6-12密码</div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">确认密码</label>
    <div class="layui-input-inline">
      <input id="newPassword2" type="password" name="newPassword2" required lay-verify="required|pass"
             placeholder="请再一次输入"
             autocomplete="off" class="layui-input">
    </div>
    <div class="layui-form-mid layui-word-aux">请输入6-12密码</div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button type="submit" lay-filter="password-submit" class="layui-btn" lay-submit>立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>
<script src="https://www.layuicdn.com/layui-v2.6.2/layui.js"></script>
<script type="text/javascript">
  layui.use(['form'], function () {
    let form = layui.form;
    let $ = layui.$;
    $.get('/getUserInfo', (data) => {
      const user = data.map.data;
      // 为密码赋值id
      form.val('password', {id: user.id});
      // 为info赋值id
      form.val('info', user);
      form.val('username', {username: user.username});
    });
    form.on('submit(info-submit)', (data) => {
      $.ajax({
        'contentType': 'application/json;charset=utf-8',
        'data': JSON.stringify(data.field),
        'url': '/updateInfo',
        'type': 'post',
        success: function (data) {
          if (data.code && data.code === 200) {
            layer.msg('修改成功');
          } else if (data.code) {
            layer.msg(data.message);
          } else {
            console.log(data)
            layer.msg('服务器异常');
          }
        }
      });
      return false;
    });

    form.verify({
      pass: [
        /^[\S]{6,12}$/
        , '密码必须6到12位，且不能出现空格'
      ]
    });

    form.on('submit(password-submit)', (data) => {
      $.ajax({
        'contentType': 'application/json;charset=utf-8',
        'data': JSON.stringify(data.field),
        'url': '/updatePassword',
        'type': 'post',
        success: function (data) {
          if (data.code && data.code === 200) {
            layer.msg('修改成功');
            setTimeout(() => {
              location.reload()
            }, 1000)
          } else {
            layer.msg(data.message);
          }
        }
      });
      return false;
    });
  });
</script>
</body>
</html>